package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.MyNewsAdapter;
import com.hongxun.design.bean.MyNewBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/11.
 * 我的消息
 */
public class MyNewsActivity extends BaseActivity implements HeadSecond, SwipeRefreshLayout.OnRefreshListener {
    SwipeRefreshListView mynews_list;
    int startNum = 0;
    int addNum = MyApp.addNum;
    private Handler experienceListHandler;
    List<MyNewBean> data;
    MyNewsAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mynews);
        findBar();
        findView();
    }

    @Override
    public void findBar() {
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.mynews_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean("", View.INVISIBLE, R.color.bar_right, ListenerSet.back, "我的消息", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();
        if (mynews_list == null)
            mynews_list = (SwipeRefreshListView) findViewById(R.id.mynews_list);
        mynews_list.setOnRefreshListener(this);
        addItemClick();

        addExperienceListHandler();
        experienceListHandler.sendEmptyMessage(1);
        onRefresh();
        MyApp.isgetMessage=false;
        MyApp.activity.Nofity();
    }

    private void addItemClick() {
        mynews_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // Bundle bundle = new Bundle();
                //bundle.putString(MString.PATH, JSON.toJSONString(data.get(i)));
                MyNewBean b = data.get(i);
                MyApp.pushClick(b);
//                Bundle bundle = new Bundle();
//                bundle.putString(MString.CODE, b.id);
//                bundle.putString(MString.TITLE, b.title);
//                bundle.putString(MString.CONTENT, b.content);
//                bundle.putString(MString.TYPE, MString.MYNEW);
//                IntentToActivity.intent(mActivity, EncyclopediasActivity.class, bundle);
            }
        });

    }

    @Override
    public void onRefresh() {
        startNum = 0;
        GetExperienceList(HttpStringUtil.SUCCESSED);
    }

    @Override
    protected void initialization() {
        super.initialization();
    }

    @Override
    public void addMore() {
        super.addMore();
        GetExperienceList(HttpStringUtil.ADDMROE);
    }


    /**
     * 数据请求*
     */
    private void GetExperienceList(int code) {
        MyApp.HTTP.SysMessList(experienceListHandler, code, startNum, startNum + addNum);
    }

    /**
     * 数据接收*
     */
    public void addExperienceListHandler() {
        experienceListHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
//                            对象类型:0 系统消息、1 项目消息
                            if (data == null) {
                                data = FastJsonUtil.getMyNewBeanList((String) msg.obj);
                            } else {
                                data.clear();
                                data.addAll(FastJsonUtil.getMyNewBeanList((String) msg.obj));
                            }
                            startNum += addNum;
                            experienceListHandler.sendEmptyMessage(1);
                        } else {
                            experienceListHandler.sendEmptyMessage(1);
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        if (data == null) data = new ArrayList<>();
                        if (mAdapter == null) {
                            mAdapter = new MyNewsAdapter(mActivity, data);
                            mynews_list.setAdapter(mAdapter);
                            mynews_list.setFragment(baseActivity);
                            mynews_list.setCanScroll(true);
                            mynews_list.setDivider(1);
                            mynews_list.setRefreshing(false);
                        } else {
                            mynews_list.update();
                            mynews_list.setRefreshing(false);
                            mynews_list.removeFooterView();
                        }
                        break;
                    case HttpStringUtil.ADDMROE:
                        data.addAll(FastJsonUtil.getMyNewBeanList((String) msg.obj));
                        startNum += addNum;
                        experienceListHandler.sendEmptyMessage(1);
                        break;
                    case HttpStringUtil.ERROR:
                        experienceListHandler.sendEmptyMessage(1);
                        mynews_list.setRefreshing(false);
                        mynews_list.removeFooterView();
                        ToastShow.showShort(mActivity, (String) msg.obj);
                        break;
                }
            }
        };
    }
}
